package com.cmw.core.ssm.service;

import com.cmw.core.base.exception.ServiceException;
import com.cmw.core.ssm.dao.GenericDaoInter;
import com.cmw.core.util.SHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 通用业务抽象类
 * @Date 2017-05-16 20:49
 * @author chengmingwei
 */
public abstract class AbsService <T,PK extends Serializable> implements IService<T, PK>{
    protected Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 实现此类时，须重写此方法
     * @param dao
     */
    public abstract GenericDaoInter<T, PK> getDao();

    public AbsService() {
    }


    @Override
    public T get(PK id) {
        try{
            T entity = getDao().get(id);
            return entity;
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public T getByPars(Map<String, Object> params)
            throws ServiceException {
        try{
            T entity = getDao().getByPars(params);
            return entity;
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public List<T> getList(Map<String, Object> map)
            throws ServiceException {
        try{
            List<T> list = getDao().getList(map);
            return list;
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public List<Map<String, Object>> getListMap(Map<String, Object> map)
            throws ServiceException {
        try{
            return getDao().getListMap(map);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void doComplexBusss(Map<String, Object> params)
            throws ServiceException {
        try{
            throw new ServiceException("doComplexBusss 方法未完现...");
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public Object doComplexBusss(SHashMap<String, Object> paramSHashMap)
            throws ServiceException {
        try{
            throw new ServiceException("doComplexBusss 方法未完现...");
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public List<T> getListAll() throws ServiceException {
        try{
            return getDao().getListAll();
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public List<T> getPageByPars(Map<String, Object> map, int offset,
                                       int pageSize) throws ServiceException {
        try{
            map.put("offset", offset);
            map.put("pageSize", pageSize);
            return getDao().getPageByPars(map);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public int insert(T obj) throws ServiceException {
        try{
            return getDao().insert(obj);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void batchInsert(List<T> objs) throws ServiceException {
        try{
            getDao().batchInsert(objs);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void update(T obj) throws ServiceException {
        try{
            getDao().update(obj);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void batchUpdate(List<T> objs) throws ServiceException {
        try{
            getDao().batchUpdate(objs);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void updateByPars(Map<String, Object> map,Map<String,Object> wherePars) throws ServiceException {
        try{
            if(null != wherePars && wherePars.size() > 0){
                Set<String> keys = wherePars.keySet();
                for(String key : keys){
                    map.put("par_"+key, wherePars.get(key));
                }
            }
            getDao().updateByPars(map);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public Long getTotals() throws ServiceException {
        try{
            return getDao().getTotals();
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public Long getTotals(Map<String, Object> param) throws ServiceException {
        try{
            return getDao().getTotalsByPars(param);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public Long getMaxID() throws ServiceException {
        try{
            return getDao().getMaxID();
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void delete(PK id) throws ServiceException {
        try{
            getDao().delete(id);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void deleteByPars(Map<String, Object> params)
            throws ServiceException {
        try{
            getDao().deleteByPars(params);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void deleteByIds(String ids) throws ServiceException {
        try{
            Map<String,Object> pars = new HashMap<String, Object>();
            pars.put("ids", ids);
            getDao().deleteByIds(pars);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void deleteAll() throws ServiceException {
        try{
            getDao().deleteAll();
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void enabled(PK id, Integer isenabled) throws ServiceException {
        try{
            Map<String,Object> params = new HashMap<String, Object>();
            params.put("id", id);
            params.put("isenabled", isenabled);
            getDao().enabled(params);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void enabledByPars(Map<String, Object> params, Integer isenabled)
            throws ServiceException {
        try{
            params.put("isenabled", isenabled);
            getDao().enabledByPars(params);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }

    @Override
    public void enabledByIds(String ids, Integer isenabled)
            throws ServiceException {
        try{
            Map<String,Object> pars = new HashMap<String, Object>();
            pars.put("ids", ids);
            pars.put("isenabled", isenabled);
            getDao().enabledByIds(pars);
        }catch(Exception ex){
            ex.printStackTrace();
            throw new ServiceException(ex);
        }
    }
}
